<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small"  :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目编码"  prop="projectNo">
        <el-input v-model.trim="queryParams.projectNo"  @change="handleQuery" placeholder="请输入项目编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model.trim="queryParams.productName" @change="handleQuery"  placeholder="请输入产品名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目经理" prop="projectManager">
        <el-select v-model="queryParams.projectManager" filterable clearable   @change="handleQuery" >
          <el-option v-for="user in projectManagerList"   :label="user.nickName" :value="user.userId" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="产品类型" prop="productType">
        <el-select v-model="queryParams.productType" filterable clearable   @change="handleQuery" >
          <el-option v-for="dict in dict.type.product_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="产品线" prop="productLine">
        <el-select v-model="queryParams.productLine" filterable clearable  @change="handleQuery" >
          <el-option v-for="dict in dict.type.product_line" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否超期" prop="overdue">
        <el-select v-model="queryParams.overdue" filterable clearable  @change="handleQuery"  >
          <el-option v-for="dict in dict.type.project_task_overdue" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="PRD OA单号" prop="prdOaNo">
        <el-input v-model.trim="queryParams.prdOaNo" @change="handleQuery"  placeholder="PRD OA单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search"   @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     
        <el-table border v-loading="loading" :data="projectList" style="margin-top:10px">
          <el-table-column label="超期天数" :show-overflow-tooltip="true" align="center" prop="overdue">
            <template slot-scope="scope">
              <el-tag  type="danger"  v-if="scope.row.overdue > 0">  {{scope.row.overdue}} 天</el-tag>
              <el-tag  v-else>未超期</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="进度" :show-overflow-tooltip="true" align="center" prop="overdue" width="150">
            <template slot-scope="scope">
              <el-progress  :percentage="scope.row.progress"  ></el-progress>
            </template>
          </el-table-column>
          
          <el-table-column label="项目编码" align="left"   prop="projectNo"  width="150">
            <template slot-scope="scope">
              <a style="color: cornflowerblue;" @click="openViewDetail(scope.row)">{{ scope.row.projectNo }}</a>
            </template>
          </el-table-column> 
          <el-table-column label="项目名称" align="center" prop="projectName"  width="150"/>
          <el-table-column label="产品名称" align="center" prop="productName"  width="150"/>
          <el-table-column label="产品类型" :show-overflow-tooltip="true" align="center" prop="productType"  width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.product_type" :value="scope.row.productType" />
            </template>
          </el-table-column>
          <el-table-column label="产品线" :show-overflow-tooltip="true" align="center" prop="productLine"  width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.product_line" :value="scope.row.productLine" />
            </template>
          </el-table-column>
          <el-table-column label="项目经理" align="center" prop="projectManagerName"  width="120"/>
          <el-table-column label="MRD OA单号" align="center" prop="mrdOaNo" width="180">
            <template slot-scope="scope">
              <a style="color: cornflowerblue;"   @click="linkToOA(scope.row.mrdOaId)"> {{ scope.row.mrdOaNo }} <i class="el-icon-link"></i></a> 
            </template>
          </el-table-column>
          <el-table-column label="PRD OA单号" align="center" prop="prdOaNo" width="150">
            <template slot-scope="scope">
              <a style="color: cornflowerblue;"   @click="linkToOA(scope.row.prdOaId)"> {{ scope.row.prdOaNo }} <i class="el-icon-link"></i></a> 
            </template>
          </el-table-column>
          <el-table-column label="状态" :show-overflow-tooltip="true" align="center" prop="status"  width="150">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.project_list_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime"  width="200">
            
          </el-table-column>
          <el-table-column label="创建人" align="center" prop="nickName" />
           
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      
  

  </div>
</template>

<script>

import {getProjectList,addProject,getDetail,modifyProject,delProject,stopProject,draftProject}  from "@/api/projectManage/projectList";
import {getTaskUsers,listUserAll,getUserProfile,getUsersByRolekey}  from "@/api/system/user";
import {signLoginToEkp}  from "@/api/oa/oa";

export default {
  name: "SearchProjectList", 
  dicts: ['project_list_status','owner_position','project_task_overdue','product_type','product_line'],
  data() {
    return {

      //弹出层loading
      dialogLoading:false,

      title:null,

      titleMember:null,

      visible:false,

      update: false,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //项目列表
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //项目成员列表
      projectMemberList:[],

      //人员列表
      userList:[],

       //项目经理列表
      projectManagerList:[],

      //当前用户
      currUser:{},


      //行号
      lineNo:0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectNo: null,
        projectManager: null,
        mrdOaNo:null,
        prdOaNo:null,
        overdue:null,
        productType:null,
        productLine:null
      },
      form:{},
      param:{},
      rules:{
          projectName: [
            { required: true, message: "项目名称不能为空", trigger: "blur" }
          ], 
          projectNo: [
            { required: true, message: "项目编号不能为空", trigger: "blur" }
          ], 
          level: [
            { required: true, message: "项目级别不能为空", trigger: "blur" }
          ], 
          mrdOaNo: [
            { required: true, message: "MRD OA单号不能为空", trigger: "blur" }
          ],
          prdOaNo: [
            { required: true, message: "PRD OA单号不能为空", trigger: "blur" }
          ],  
        },
        rulesMember:{
          type: [
            { required: true, message: "岗位不能为空", trigger: "blur" }
          ], 
          userId: [
            { required: true, message: "用户不能为空", trigger: "blur" }
          ], 
        }
    };
  },
  created() {
    this.getList();
    this.getCurrUserInfo();
    this.handleTaskUsers();
    this.getProjectManagerList();
    
  },
 
  methods: {

   getProjectManagerList(){
      //外部供应商
      getUsersByRolekey(this.$store.state.globalParam.PM).then(result => {
          this.projectManagerList = result.data             
      })

   },

 
 

   //查看OA表单详情
   linkToOA(oaId){
      signLoginToEkp(oaId).then(response=>{
        if(response.code == 200){
          window.open(response.data.authorizationURL, '_blank');
        }else{
          this.$alert(response.data.msg, '错误');
        }  
                   
      })
    },



    //选中PRD 流程
    handleSelPrd(record){

        this.form = record;
        this.form.projectManager = this.currUser.userId;
        this.form.projectManagerName = this.currUser.nickName;
        
        console.log(this.form);
 
    },


    //设置时间
    handleSettime(row){
        this.$refs.settime.open();  
        this.$refs.settime.init('设置项目时间',row.id);
    },


    //选择PRD
    handlePrd(){
        this.$refs.prd.open();  
    },



    //项目详情
    openViewDetail(row){
      this.$nextTick(() => {
        this.$router.push({
          path: "ProjectDetail",
          query: row
        })
      })

    },
 

    closeOpen() {
      this.open = false;
      this.form={};
      this.update = false;
      this.projectMemberList=[];
    },


    //保存草稿
    onDraft(){
       this.$modal.loading("请稍候...");
       this.form.projectListMemberList = this.projectMemberList;
       draftProject(this.form).then(response => {
        this.getList();
        this.$modal.closeLoading();
        this.closeOpen(); 
      });
    },



    //加载当前用户信息
    getCurrUserInfo(){
       getUserProfile().then(response => {
           this.currUser = response.data;
           
      });
    },


    //提交
    onSure(){
      this.$modal.loading("请稍候...");
       this.form.projectListMemberList = this.projectMemberList;
       addProject(this.form).then(response => {
        this.getList();
        this.$modal.closeLoading();
        this.closeOpen(); 
      });
    },

    //提交修改
    onModify(){
       this.$modal.loading("请稍候...");
       this.form.projectListMemberList = this.projectMemberList;
       //如果是修改草稿提交 则是直接提交项目
       if(this.form.status == 0 && this.update){
          this.form.status = 10;
       }
       modifyProject(this.form).then(response => {
        this.getList();
        this.$modal.closeLoading();
        this.closeOpen(); 
      });
    },




    //删除项目成员
    handleDeleteMember(row){
 
      let  projectMemberListTwo = [];
      this.projectMemberList = this.projectMemberList.filter(i=> i.lineNo !=row.lineNo );
 
      this.projectMemberList.forEach((item,index)=>{
            item.lineNo = (index+1);
            projectMemberListTwo.push(item);
      })
      this.projectMemberList = projectMemberListTwo;
    },
    //加载参加任务的人员列表
    handleTaskUsers(){

      getTaskUsers().then(response => {
        this.userList = response.data;
      });
    },
   
    //添加成员
    handelAddMember(){
      // this.titleMember = '添加成员';
      // this.visible= true;  
      let lineNoIndex = this.projectMemberList.length+1;
      let item = {
        lineNo:lineNoIndex, 
        type:'',
        userId:''

      };
      this.projectMemberList.push(item);
    },

    //关闭添加成员弹出框
    handelCloseMember(){
      this.visible= false;
      this.param= {};  
    },


    changeStatus(event) {
      this.queryParams.status = event
      this.getList()
    },
    /** 查询项目列表 */
    getList() {
      this.queryParams.exportFlag=null
      this.loading = true;
      getProjectList(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
 
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
 
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "创建项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改项目数据";
      this.update = true;
      this.dialogLoading = true;
      listUserAll().then(response=>{
          this.userList = response.data;
          //获取项目详情
          getDetail(row.id).then(response =>{
              this.dialogLoading = false;
              this.form = response.data;
              this.projectMemberList = response.data.projectListMemberList;
          })
      })      
    },
 
    /** 删除按钮操作 */
    handleDel(row) {
       
      this.$modal.confirm('是否确认删除项目编号为"' + row.projectNo + '"的数据项？').then(function () {
        return delProject(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.exportFlag="1"
      this.download('/business/project/export', {
        ...this.queryParams
      }, `${new Date().getTime()}_项目数据.xlsx`)
      
    },
    //项目中止
    handleStopProject(row) {
      
      this.$modal.confirm('是否确认中止项目编号为"' + row.projectNo + '"的数据项？').then(function () {
        return stopProject(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功!");
      }).catch(() => { });
       
    }
  }
};
</script>
<style scoped>
.el-col {
  height: 60px;
}

.labelword ::v-deep .el-form-item__label {
  white-space: pre-line !important;
  line-height: 15px !important;
}

.el-table .danger-row {
  background: rgb(187, 179, 184);
}

.el-table .success-row {
  background: #00FF00;
}
</style>
